package com.anhry.app.safety.manage.enterprise.struts.action;

import java.io.File;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.enterprise.bean.TbCorpPic;
import com.anhry.app.safety.manage.enterprise.struts.base.EnterpriseBaseAction;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.UploadFile;
import com.anhry.app.safety.util.Util;
import com.anhry.app.safety.util.UtilMethod;

/**
 * @date:Jul 25, 2012
 * @remark:
 * @author:dongbin
 */
@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class CorpPicAction extends EnterpriseBaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String corpId;
	private String picType;//图片类型
	private String picTitle;//标题
	private String picRemark;//图片说明
	private File pic;
	private String picContentType;
	private String picFileName;
	
	/**
	 * 企业图片
	 * @return
	 */
	public String list(){
		try {
			if(null!=this.corpId && this.corpId.length()>0){
				this.setObjList((List)this.utilService.getHqlQuery("from TbCorpPic t where 1=1 and t.corpId="+this.corpId+" order by t.id"));
				this.setResult("corpPic_list");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpPicAction:list() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 图片录入页面
	 * @return
	 */
	public String edit(){
		this.setResult("corpPic_edit");
		return SUCCESS;
	}
	
	/**
	 * 图片保存
	 * @return
	 */
	public String save(){
		try {
			if(null!=this.corpId && this.corpId.length()>0){
				if(null!=this.pic){
					if(!UtilMethod.filterType(this.picContentType, Util.ALLOWTYPES.split(","))){
						this.setTip("图片格式错误！");
						return this.edit();
					}else{
						String path = Util.SAFETY_FILE+File.separator+Util.FILE_FOLDER+File.separator+Util.CORP;
						String fileName = UploadFile.copy(this.pic, path, this.picFileName, this.corpId);
						String path1 = Util.FILE_FOLDER+File.separator+Util.CORP+File.separator+this.corpId+File.separator+fileName;
						TbCorpPic pic = new TbCorpPic();
						pic.setIsDele("0");
						pic.setPicPath(path1);
						pic.setPicRemark(this.picRemark);
						pic.setPicTitle(this.picTitle);
						pic.setPicType(this.picType);
						pic.setCorpId(Long.parseLong(this.corpId));
						this.utilService.save(pic);
						this.setTip("添加成功！");
					}
				}
				return this.list();
			}
		} catch (Exception e) {
			Loggers.error("manager:CorpPicAction:save()方法出错："+e.getMessage());
			e.printStackTrace();
		}
		this.setTip(Util.NOTIP);
		return ERROR;
	}
	
	/**
	 * 异步删除图片
	 *
	 */
	public void deleFj(){
		this.contextPvd.getResponse().setContentType("charset=UTF-8");
		try {
			if(null!=this.objId && this.objId.length()>0){
				TbCorpPic fj = (TbCorpPic)this.utilService.getObjectById(TbCorpPic.class, Long.parseLong(this.objId));
				if(null!=fj){
					UploadFile.removeFile(Util.SAFETY_FILE+File.separator+fj.getPicPath());
					this.utilService.delete(fj);
				}
				this.contextPvd.getResponse().getWriter().write("1");
			}else{
				this.contextPvd.getResponse().getWriter().write("2");
			}
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("manager:CorpPicAction:deleFj()方法出错："+e.getMessage());
		}
	}
	
	public String getCorpId() {
		return corpId;
	}

	public void setCorpId(String corpId) {
		this.corpId = corpId;
	}

	public String getPicType() {
		return picType;
	}

	public void setPicType(String picType) {
		this.picType = picType;
	}

	public String getPicTitle() {
		return picTitle;
	}

	public void setPicTitle(String picTitle) {
		this.picTitle = picTitle;
	}

	public String getPicRemark() {
		return picRemark;
	}

	public void setPicRemark(String picRemark) {
		this.picRemark = picRemark;
	}

	public File getPic() {
		return pic;
	}

	public void setPic(File pic) {
		this.pic = pic;
	}

	public String getPicContentType() {
		return picContentType;
	}

	public void setPicContentType(String picContentType) {
		this.picContentType = picContentType;
	}

	public String getPicFileName() {
		return picFileName;
	}

	public void setPicFileName(String picFileName) {
		this.picFileName = picFileName;
	}
	

}
